package com.definity.toolkit.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.serialization.Serializer;
import br.com.caelum.vraptor.view.Results;

import com.definity.security.common.User;
import com.definity.toolkit.i18n.I18n;
import com.definity.toolkit.i18n.I18nFactory;
import com.definity.toolkit.message.Message;
import com.definity.toolkit.message.UnexpectedErrorMessage;

public abstract class DWTController {

	protected final Logger logger = Logger.getLogger(getClass());
	
	private final List<Message> messages = new ArrayList<Message>();
	private final I18n i18n = I18nFactory.create("form");
	protected final HttpServletRequest request;
	protected final Result result;
	
	public DWTController() {
		request = null;
		result = null;
	}

	@SuppressWarnings("unchecked")
	public DWTController(Result result, HttpServletRequest request) {
		this.result = result;
		this.request = request;
		
		List<Message> messageList = (List<Message>) request.getAttribute("messages");
		if (messageList != null)
			messages.addAll(messageList);
		
		result.include("url", request.getRequestURI());
		result.include("messages", messages);
	}
	
	protected List<Message> getMessages() {
		return messages;
	}
	
	protected void addMessage(Message message) {
		messages.add(message);
	}
	
	protected boolean validate(Object value) {
		if (value instanceof String) {
			return value != null && !((String) value).trim().isEmpty();
		}
		return value != null;
	}
	
	protected User getCurrentUser() {
		return (User) request.getSession().getAttribute("dwtuser");
	}
	
	protected void setCurrentUser(User user) {
		request.getSession().setAttribute("dwtuser", user);
	}
	
	protected String getLanguage() {
		return (String) request.getSession().getAttribute("lang");
	}
	
	protected void setLanguage(String language) {
		request.getSession().setAttribute("lang", language);
	}
	
	protected String i18n(String key) {
		return i18n.tl(key);
	}
	
	protected void redirectToError() {
		StringBuilder builder = new StringBuilder();
		
		List<Message> messages = getMessages();
		if (messages.isEmpty())
			messages.add(new UnexpectedErrorMessage());
		
		for (Message message : messages) {
			builder.append("<li>");
			builder.append(message.getMessage());
			builder.append("</li>");
		}
		
		result.use(Results.http()).sendError(500, builder.toString());
	}
	
	protected void redirectTo(String url) {
		url = url.replace(request.getContextPath(), "");
		result.redirectTo(url);
	}
	
	protected <T> T redirectTo(Class<T> type) {
		return result.use(Results.logic()).redirectTo(type);
	}

	protected void forwardTo(String url) {
		url = url.replace(request.getContextPath(), "");
		result.use(Results.page()).forwardTo(url);
	}

	protected <T> T forwardTo(Class<T> type) {
		return result.use(Results.logic()).forwardTo(type);
	}
	
	protected <T> Serializer json(T type) {
		return result.use(Results.json()).withoutRoot().from(type);
	}

	protected void doNothing() {
		result.use(Results.nothing());
	}

}
